# !/usr/bin bash
echo "=========当前部署为测试环境，请检查是否正确========="

echo "服务器信息："
echo "后台管理容器：zbq-mall-manager    端口：8090"
echo "API接口容器：zbq-mall-api        端口：8090"
echo "资源目录：/opt/volume"
echo "redis配置：/opt/volume/redis"
echo "nginx配置：/opt/volume/nginx"



# 判断文件是否存在 不存在则退出
if [[ ! -f zbq-mall-manager/target/app.jar ]]; then
  echo "=========没有找到部署文件，请先执行项目打包。==========="
  exit 1
fi

# 判断文件是否存在 不存在则退出
if [[ ! -f zbq-mall-api/target/app.jar ]]; then
  echo "=========没有找到部署文件，请先执行项目打包。==========="
  exit 1
fi

echo "确定部署文件是否最新？部署环境是否正确？(y/n)"
read check
if [[ $check != 'y' ]]; then
  echo "部署终止。"
  exit 1
fi

echo "请输入需要部署的项目？(0:all,1:api.2:manager，默认0)"
read projectType



echo "请输入您的服务器用户名："
read username
if [[ ! $username ]]; then
  echo "$0 username is required"
  exit 1
fi

remotessh=${username}@121.40.231.153

#设置一下
stty -echo
echo "请输入您的服务器密码："
read password
stty echo
if [[ ! $password ]]; then
  echo "$0 password is required"
  exit 1
fi

if [[  $projectType == '' ||$projectType == '0' || $projectType == '2' ]]; then
  echo '开始传输管理后台安装包...'
  sshpass -p "$password" scp zbq-mall-manager/target/app.jar ${remotessh}:/home/deploy/mall-manager/app.jar
  echo '管理后台安装包传输完成。'
fi

if [[  $projectType == '' ||$projectType == '0' || $projectType == '1' ]]; then
  echo '开始传输 API接口 安装包...'
  sshpass -p "$password" scp zbq-mall-api/target/app.jar ${remotessh}:/home/deploy/mall-api/app.jar
fi

echo '所有安装包传输完成，准备就绪.'

echo '开始项目部署...'
if [[  $projectType == '' ||$projectType == '0' ]]; then
  sshpass -p "$password" ssh ${remotessh} "cd /home/deploy ; sh run.sh"
fi

if [[ $projectType == '1' ]]; then
  sshpass -p "$password" ssh ${remotessh} "cd /home/deploy/mall-api ; sh restart.sh"
fi

if [[ $projectType == '2' ]]; then
  sshpass -p "$password" ssh ${remotessh} "cd /home/deploy/mall-manager ; sh restart.sh"
fi

echo '项目部署完成.'
